package com.fy56.platform.logistics.service.impl;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.mapper.ReceiverContactInfoMapper;
import com.fy56.platform.logistics.mapper.SenderContactInfoMapper;
import com.fy56.platform.logistics.model.ReceiverContactInfo;
import com.fy56.platform.logistics.model.SenderContactInfo;
import com.fy56.platform.logistics.service.wx.ISenderContactInfoService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 发货联系人Service实现
 * @author li
 * @date 2018-05-30 16:58
 */
@Service
public class SenderContactInfoServiceImpl implements ISenderContactInfoService {

    @Autowired
    private SenderContactInfoMapper senderContactInfoMapper;
    @Autowired
    private ReceiverContactInfoMapper receiverContactInfoMapper;

    /**
     * 根据用户ID获取发货联系人
     *
     * @param vipMemberId
     * @return
     */
    @Override
    public List<SenderContactInfo> selectByVipMemberId(Long vipMemberId){
        return senderContactInfoMapper.selectByVipMemberId(vipMemberId);
    }

    @Override
    public SenderContactInfo getExistsByVipMemberIdAndMobile(SenderContactInfo record){
        return senderContactInfoMapper.getExistsByVipMemberIdAndMobile(record);
    }

    @Override
    public SenderContactInfo selectByPrimaryKey(Long id) {
        return senderContactInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public SenderContactInfo get(SenderContactInfo entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insert(SenderContactInfo entity) {
        ResultModel result = null;

        if(entity.getIsDefault() == 1){
            //更新所有发货联系人记录的是否默认为 0
            senderContactInfoMapper.updateDefaultFlag(entity.getVipMemberId());
        }
        int r = senderContactInfoMapper.insert(entity);
        if(r == 0) {
            result = ResultModel.error(ResultStatus.INSERT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultModel insertSelective(SenderContactInfo entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel update(SenderContactInfo entity) {
        ResultModel result = null;
        if(entity.getIsDefault() == 1){
            //更新所有发货联系人记录的是否默认为 0
            senderContactInfoMapper.updateDefaultFlag(entity.getVipMemberId());
        }
        int r = senderContactInfoMapper.updateByPrimaryKey(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(SenderContactInfo entity) {
        ResultModel result = null;
        int r = senderContactInfoMapper.updateByPrimaryKeySelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Long id) {
        int r = senderContactInfoMapper.deleteByPrimaryKey(id);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(SenderContactInfo entity) {
        int r = senderContactInfoMapper.updateByPrimaryKeySelective(entity);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    public List<SenderContactInfo> findList(SenderContactInfo entity) {
        List<SenderContactInfo> list = senderContactInfoMapper.findList(entity);
        return list;
    }

    @Override
    public Map<String, Object> getDefaultReceiverSenderByUserIdService(Long userId){
        Map<String, Object> result = Maps.newHashMap();

        //1 查询设置,默认收货人信息
        List<ReceiverContactInfo> receivers = receiverContactInfoMapper.selectByVipMemberId(userId);
        result.put("receiverContactInfos",receivers);

        // 默认收货人
        for(ReceiverContactInfo receiver : receivers){
            if(receiver.getIsDefault() == 1){
                result.put("defaultReceiver",receiver);
                break;
            }
        }

        //2 查询设置，默认发货人信息
        List<SenderContactInfo> senders = senderContactInfoMapper.selectByVipMemberId(userId);
        result.put("senderContactInfos",senders);
        //设置默认发货人
        for(SenderContactInfo sender : senders){
            if(sender.getIsDefault() == 1){
                result.put("defaultSender",sender);
                break;
            }
        }
        return result;
    }


}
